package com.tsh.core.cloud.feign;

import com.tsh.core.tool.constant.TshConstant;
import com.tsh.core.tool.utils.ThreadLocalUtil;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;

@Configuration
public class FeignConfig{
	@Bean("requestInterceptor")
	public RequestInterceptor requestInterceptor(){
		return new RequestInterceptor() {
			@Override
			public void apply(RequestTemplate requestTemplate) {
				HttpHeaders headers = ThreadLocalUtil.get(TshConstant.CONTEXT_KEY);
				if (headers != null && !headers.isEmpty()) {
					headers.forEach((key, values) ->
						values.forEach(value -> requestTemplate.header(key, value))
					);
				}
			}
		};
	}
}

